package edu.cuit.avatar.thread;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author <a href="mailto:1020zhaodan@163.com">Adan</a>
 * @version 1.0
 * @date 2025/7/16 10:32
 */
public class CallableAndFutureDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask<String> task = new FutureTask<>(()->{
            for (var i = 0; i < 100; i++){
                System.out.println(" " + i);
                if(i == 20 || i == 50)
                    Thread.sleep(2000);
            }
            return "小猪Peppa来了";
        });

        Thread thread = new Thread(task);
        thread.start();

        System.out.println("任务执行结果:" + task.get());
        System.out.println(Thread.currentThread().getName());
    }
}
